package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseVersion;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Version extends BaseVersion<Version> {

  public static final Version dao = new Version().dao();

  public static ModelResult add(int file, String version,
      String content) {
    ModelResult result = new ModelResult(Result.kModelSaveError);
    JSONArray accessory = new JSONArray();
    String ver = Version.version(version, 1);
    Version v = new Version();
    if (file > 0) {
      v.setFile(file);
      accessory.add(file);
    }
    v.setVersion(ver);
    v.setContent(content.replaceAll("\n", "\n<br>"));
    boolean r = v.save();
    if (r && accessory.size() > 0) {
      r = FileModel.add(4, v.getId(), accessory, "accessory");
    }
    if (r) {
      result.setResult(Result.kSuccess);
      result.setObject(v);
    }
    return result;
  }

  public static ModelResult update(int id, int file, String version,
      String content) {
    ModelResult result = new ModelResult(Result.kModelUpdateError);
    JSONArray accessory = new JSONArray();
    Version v = Version.dao.findById(id);
    if (null != v) {
      v.setFile(file);
      v.setVersion(version);
      v.setContent(content.replaceAll("\n", "\n<br>"));
      boolean r = v.update();
      if (r) {
        if (accessory.size() > 0) {
          r = FileModel.update(4, v.getId(), accessory, "accessory");
        }
        if (r) {
          result.setResult(Result.kSuccess);
          result.setObject(v);
        }
      }
    }
    return result;
  }

  public static boolean delete(int id) {
    return Version.dao.deleteById(id);
  }

  /*版本更新名字去重
  去重规则：if version.name(0.5.1) exist --->(0.5.1(1)) --->(0.5.1(2))
  */
  public static String version(String version, int i) {
    Version ver = dao.findFirst(
        "select * from version where version= '" + version + "'");
    if (null == ver) {
      return version;
    } else {
      version = version.contains("(")
                ? version.substring(0, version.indexOf("(")) + "(" + i + ")"
                : version + "(" + i + ")";
      return Version.version(version, i + 1);
    }
  }

  public static JSONObject list(int page, int size) {

    String select = Db.getSql("version-list");
    String except = Db.getSql("version-list-except");
    Page pages = Version.dao.paginate(page, size, select, except);
    return Models.compressPage(pages);

  }

  public static ModelResult query(String version) {
    ModelResult result = new ModelResult();
    int frist = Integer.parseInt(StringUtils.substringBefore(version, "."));
    int second = Integer.parseInt(StringUtils
        .substringBefore(StringUtils.substringAfter(version, "."), "."));
    int third = Integer.parseInt(StringUtils
        .substringAfter(StringUtils.substringAfter(version, "."), "."));
    List<Record> list = Db
        .find(Db.getSql("version-query"), frist, frist, second, frist,
            second, third);

    if (null != list && list.size() > 0) {
      JSONObject object = new JSONObject();
      object.put("version", Models.compress(list.get(0)));
      list.remove(0);
      object.put("history", Models.compressLists(list));

      result.setObject(object);
    }
    result.setResult(Consts.Result.kSuccess);

    return result;
  }
}
